home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 3.0a2 / Libraries / UCommand.cp < prev    next >
Text File  |  1991-05-01  |  14KB  |  509 lines

  1. // UCommand.cp 
  2. /* Copyright © 1985-1991 by Apple Computer, Inc.  All rights reserved. */
  3.  
  4. #ifndef __UCOMMAND__
  5. #include <UCommand.h>
  6. #endif
  7.  
  8. #ifndef __STDIO__
  9. #include <StdIo.h>
  10. #endif
  11.  
  12. #ifndef __UGEOMETRY__
  13. #include <UGeometry.h>
  14. #endif
  15.  
  16. #ifndef __UAPPLEEVENTS__
  17. #include <UAppleEvents.h>
  18. #endif
  19.  
  20. #ifndef __UDOCUMENT__
  21. #include <UDocument.h>
  22. #endif
  23.  
  24. #ifndef __UAPPLICATION__
  25. #include <UApplication.h>
  26. #endif
  27.  
  28. #ifndef __USCROLLER__
  29. #include <UScroller.h>
  30. #endif
  31.  
  32. #ifndef __UMACAPPUTILITIES__
  33. #include <UMacAppUtilities.h>
  34. #endif
  35.  
  36. #ifndef __UMACAPPGLOBALS__
  37. #include <UMacAppGlobals.h>
  38. #endif
  39.  
  40. #ifndef __UMEMORY__
  41. #include <UMemory.h>
  42. #endif
  43.  
  44. #ifndef __UERRORMGR__
  45. #include <UErrorMgr.h>
  46. #endif
  47.  
  48.  
  49.  
  50. //--------------------------------------------------------------------------------------------------
  51. #pragma segment MACommandRes
  52.  
  53. pascal void TCommand::ICommand(CmdNumber itsCmdNumber,
  54.                                TObject* notify,
  55.                                TView* itsView)
  56. {
  57.     this->IEvent(itsCmdNumber,NULL,gApplication);
  58.  
  59.     fID = itsCmdNumber;
  60.     fChangedObject = notify;
  61.     fView = itsView;
  62. }
  63.  
  64. //--------------------------------------------------------------------------------------------------
  65. #pragma segment MACommandRes
  66.  
  67. pascal void TCommand::Initialize(void)            // override 
  68. {
  69.     inherited::Initialize();
  70.     
  71.     fChangedObject = NULL;
  72.     fCmdDone = FALSE;
  73.     fCanUndo = TRUE;
  74.     fCausesChange = TRUE;
  75.     fChangesClipboard = FALSE;
  76.     fFreeOnCompletion = TRUE;                    // command will be freed after DoIt or Commit
  77.     fRecurring = FALSE;                            // Command is a one-shot deal 
  78.     fReadyToExecute = TRUE;                        // Always executable by default 
  79.     fView = NULL;
  80. }
  81.  
  82. //--------------------------------------------------------------------------------------------------
  83. #pragma segment MADoCommand
  84.  
  85. pascal void TCommand::Commit(void)
  86. {
  87. }
  88.  
  89. //--------------------------------------------------------------------------------------------------
  90. #pragma segment MADoCommand
  91.  
  92. pascal void TCommand::DoIt(void)
  93. {
  94. }
  95.  
  96. //--------------------------------------------------------------------------------------------------
  97. #pragma segment MACommandRes
  98.  
  99. pascal ChangeID TCommand::GetChangeID(void)
  100. {
  101.     if (fCmdDone)
  102.         return fID;
  103.     else
  104.         return cUndo;
  105. }
  106.  
  107. //--------------------------------------------------------------------------------------------------
  108. #pragma segment MACommandRes
  109.  
  110. pascal Boolean TCommand::IsReadyToExecute(void)    // override 
  111. {
  112.     return fReadyToExecute;
  113. }
  114.  
  115. //--------------------------------------------------------------------------------------------------
  116. #pragma segment MACommandRes
  117.  
  118. pascal Boolean TCommand::IsRecurring(void)        // override 
  119. {
  120.     return fRecurring;
  121. }
  122.  
  123. //--------------------------------------------------------------------------------------------------
  124. #pragma segment MADoCommand
  125.  
  126. pascal void TCommand::RedoIt(void)
  127. {
  128. }
  129.  
  130. //--------------------------------------------------------------------------------------------------
  131. #pragma segment MACommandRes
  132.  
  133. pascal Boolean TCommand::ShouldFreeOnCompletion(void)// override 
  134. {
  135.     return fFreeOnCompletion;
  136. }
  137.  
  138.  
  139. //--------------------------------------------------------------------------------------------------
  140. #pragma segment MADoCommand
  141.  
  142. pascal void TCommand::UndoIt(void)
  143. {
  144. }
  145.  
  146. //--------------------------------------------------------------------------------------------------
  147. #pragma segment MACommandRes
  148.  
  149. pascal void TCommand::Process(void)    
  150. {
  151.     TEvtHandler* handler = this->GetHandler();
  152.     
  153.     if (handler)
  154.         handler->PerformCommand(this);
  155. }
  156.  
  157. //--------------------------------------------------------------------------------------------------
  158. #pragma segment MAFields
  159.  
  160. pascal void TCommand::Fields(TObject* obj)        // override 
  161. {
  162.     obj->DoToField("TCommand", (Ptr)NULL, bClass);
  163.     obj->DoToField("fCanUndo", (Ptr) & fCanUndo, bBoolean);
  164.     obj->DoToField("fCausesChange", (Ptr) & fCausesChange, bBoolean);
  165.     obj->DoToField("fChangedObject", (Ptr) & fChangedObject, bObject);
  166.     obj->DoToField("fChangesClipboard", (Ptr) & fChangesClipboard, bBoolean);
  167.     obj->DoToField("fCmdDone", (Ptr) & fCmdDone, bBoolean);
  168.     obj->DoToField("fFreeOnCompletion", (Ptr) & fFreeOnCompletion, bBoolean);
  169.     obj->DoToField("fReadyToExecute", (Ptr) & fReadyToExecute, bBoolean);
  170.     obj->DoToField("fRecurring", (Ptr) & fRecurring, bBoolean);
  171.     obj->DoToField("fView", (Ptr) & fView, bObject);
  172.  
  173.     inherited::Fields(obj);
  174. }
  175.  
  176. //--------------------------------------------------------------------------------------------------
  177. #pragma segment MACommandRes
  178.  
  179. pascal void TNoChangesCommand::INoChangesCommand(CmdNumber itsCmdNumber,
  180.                                                  TObject* notify,
  181.                                                  TView* itsView)
  182. {
  183.     this->ICommand(itsCmdNumber, notify, itsView);
  184.  
  185.     // we said NO changes 
  186.     fCanUndo = FALSE;
  187.     fCausesChange = FALSE;
  188. }
  189.  
  190. //--------------------------------------------------------------------------------------------------
  191. #pragma segment MAFields
  192.  
  193. pascal void TNoChangesCommand::Fields(TObject* obj)// override 
  194. {
  195.     obj->DoToField("TNoChangesCommand", (Ptr)NULL, bClass);
  196.  
  197.     inherited::Fields(obj);
  198. }
  199.  
  200. //--------------------------------------------------------------------------------------------------
  201. #pragma segment MACommandRes
  202.  
  203. pascal void TTracker::Initialize(void)            // override 
  204. {
  205.     inherited::Initialize();
  206.     
  207.     fConstrainsMouse = FALSE;
  208.     fInitialPt = gZeroVPt;
  209.     fScroller = NULL;
  210.     fTrackNonMovement = FALSE;
  211.     fTracksMouse = TRUE;                        // After all, we came here to track the mouse. 
  212.     fViewConstrain = TRUE;
  213. }
  214.  
  215. //--------------------------------------------------------------------------------------------------
  216. #pragma segment MACommandRes
  217.  
  218. pascal void TTracker::ITracker(CmdNumber itsCmdNumber,
  219.                                TObject* notify,
  220.                                TView* itsView,
  221.                                TScroller* itsScroller,
  222.                                const VPoint& itsMouse)
  223. {
  224.     this->ICommand(itsCmdNumber, notify, itsView);
  225.     fInitialPt = itsMouse;
  226.     fScroller = itsScroller;
  227. }
  228.  
  229. //--------------------------------------------------------------------------------------------------
  230. #pragma segment MADoCommand
  231.  
  232. pascal void TTracker::AutoScroll(const VPoint& delta)
  233. {
  234.     if (fScroller)
  235.         fScroller->ScrollBy(delta, kRedraw);
  236.     else
  237.     {
  238.         //??? any sensible behaviour here? 
  239.     }
  240. }
  241.  
  242. //--------------------------------------------------------------------------------------------------
  243. #pragma segment MADoCommand
  244.  
  245. pascal Boolean TTracker::IsDoneTracking(void)
  246. {
  247.     if (fView)
  248.         return fView->IsDoneTracking();
  249.     else
  250.         //??? Would it be better to let the application have a crack at it here?
  251.         return !StillDown();
  252. }
  253.  
  254. //--------------------------------------------------------------------------------------------------
  255. #pragma segment MACommandRes
  256.  
  257. pascal void TTracker::Process(void) // OVERRIDE
  258. {
  259.     TCommand* finalCommand = NULL;
  260.     TEvtHandler* handler = NULL;
  261. #if qDebug
  262.     MAName aMAName;
  263. #endif
  264.     
  265.     if (this->fTracksMouse)
  266.     {
  267. #if qDebugMsg
  268.         if (gIntenseDebugging && this)
  269.         {
  270.             this->GetClassName(aMAName);
  271.             fprintf(stderr, "Tracking Command: %s\n", (char *) aMAName);
  272.         }
  273. #endif
  274.         if (gApplication->fEventLevel == 1)    // Don't unload segs if in nested event handling 
  275.             UnloadAllSegments();
  276.         finalCommand = gApplication->TrackMouse(fInitialPt.Copy(), gStdHysteresis, this);
  277.         
  278.         if (finalCommand)
  279.         {
  280.             handler = finalCommand->GetHandler();
  281.             if (handler)
  282.                 handler->PerformCommand(finalCommand);
  283.         }
  284.     }
  285. }
  286.  
  287. //--------------------------------------------------------------------------------------------------
  288. #pragma segment MADoCommand
  289.  
  290. pascal void TTracker::TrackConstrain(TrackPhase aTrackPhase,
  291.                                         const VPoint& anchorPoint,
  292.                                           const VPoint& previousPoint,
  293.                                         VPoint& nextPoint,
  294.                                         Boolean mouseDidMove)
  295. {
  296.     if (fView)
  297.         fView->TrackConstrain(aTrackPhase, anchorPoint, previousPoint, nextPoint, mouseDidMove);
  298.     else
  299.     {
  300.         //??? should this call back into an application method if there is no view? 
  301.     }
  302. }
  303.  
  304. //--------------------------------------------------------------------------------------------------
  305. #pragma segment MADoCommand
  306.  
  307. pascal void TTracker::TrackFeedback(TrackPhase aTrackPhase,
  308.                                         const VPoint& anchorPoint,
  309.                                         const VPoint& previousPoint,
  310.                                         const VPoint& nextPoint,
  311.                                         Boolean mouseDidMove,
  312.                                         Boolean turnItOn)
  313. {
  314.     if (fView)
  315.         fView->TrackFeedback(aTrackPhase, anchorPoint, previousPoint, nextPoint, mouseDidMove, turnItOn);
  316.     else
  317.     {
  318.         //??? should this call back into an application method if there is no view? 
  319.     }
  320. }
  321.  
  322. //--------------------------------------------------------------------------------------------------
  323. #pragma segment MADoCommand
  324.  
  325. pascal TTracker* TTracker::TrackMouse(TrackPhase aTrackPhase,
  326.                                         VPoint& anchorPoint,
  327.                                         VPoint& previousPoint,
  328.                                         VPoint& nextPoint,
  329.                                         Boolean mouseDidMove)
  330. {
  331.     if (fView)
  332.         fView->TrackMouse(aTrackPhase, anchorPoint, previousPoint, nextPoint, mouseDidMove);
  333.     else
  334.     {
  335.         //??? should this call back into an application method if there is no view? 
  336.     }
  337.  
  338.     if ((aTrackPhase == trackRelease) && ((fView == NULL) ||!(fView->ContainsMouse(nextPoint))))
  339.         return NULL;
  340.     else
  341.         return this;
  342. }
  343.  
  344. //--------------------------------------------------------------------------------------------------
  345. #pragma segment MAFields
  346.  
  347. pascal void TTracker::Fields(TObject* obj)        // override 
  348. {
  349.     obj->DoToField("TTracker", (Ptr)NULL, bClass);
  350.     obj->DoToField("fConstrainsMouse", (Ptr) & fConstrainsMouse, bBoolean);
  351.     obj->DoToField("fInitialPt", (Ptr) & fInitialPt, bVPoint);
  352.     obj->DoToField("fScroller", (Ptr) & fScroller, bObject);
  353.     obj->DoToField("fTrackNonMovement", (Ptr) & fTrackNonMovement, bBoolean);
  354.     obj->DoToField("fTracksMouse", (Ptr) & fTracksMouse, bBoolean);
  355.     obj->DoToField("fViewConstrain", (Ptr) & fViewConstrain, bBoolean);
  356.  
  357.     inherited::Fields(obj);
  358. }
  359.  
  360.  
  361. //--------------------------------------------------------------------------------------------------
  362. #pragma segment MACommandRes
  363.  
  364. pascal void TNoChangesTracker::INoChangesTracker(CmdNumber itsCmdNumber,
  365.                                                  TObject* notify,
  366.                                                  TView* itsView,
  367.                                                  TScroller* itsScroller,
  368.                                                  const VPoint& itsMouse)
  369. {
  370.     this->ITracker(itsCmdNumber, notify, itsView, itsScroller, itsMouse);
  371.  
  372.     // we said NO changes 
  373.     fCanUndo = FALSE;
  374.     fCausesChange = FALSE;
  375. }
  376.  
  377. //--------------------------------------------------------------------------------------------------
  378. #pragma segment MAFields
  379.  
  380. pascal void TNoChangesTracker::Fields(TObject* obj)// override 
  381. {
  382.     obj->DoToField("TNoChangesTracker", (Ptr)NULL, bClass);
  383.  
  384.     inherited::Fields(obj);
  385. }
  386.  
  387.  
  388. //--------------------------------------------------------------------------------------------------
  389. #pragma segment MASelCommand
  390.  
  391. pascal void TAppleCommand::IAppleCommand(CmdNumber itsCmdNumber,
  392.                                          TDocument* itsDocument,
  393.                                          TView* itsView)
  394. {
  395.     this->ICommand(itsCmdNumber, itsDocument, itsView);
  396. }
  397.  
  398. //--------------------------------------------------------------------------------------------------
  399. #pragma segment MASelCommand
  400.  
  401. pascal void TAppleCommand::InitFromAppleEvent(CmdNumber itsCmdNumber,
  402.                                               TDocument* itsDocument,
  403.                                               TView* itsView,
  404.                                               const AppleEvent& itsMessage,
  405.                                               const AppleEvent& itsReply)
  406. {
  407.     FailInfo fi;
  408.     
  409.     this->IAppleCommand(itsCmdNumber, itsDocument, itsView);
  410.  
  411.     if (fi.Try())
  412.     {
  413.         fMessage = new TAppleEvent;
  414.         fMessage->InitFromMessage(itsMessage);
  415.  
  416.         fReply = new TAppleEvent;
  417.         fReply->InitFromMessage(itsReply);
  418.         
  419.         // Inform the AppleEvent manager that this event will be processed asynchronously.
  420.         // The corresponding resume will occur in TAppleCommand::Free().
  421.         FailOSErr(AESuspendTheCurrentEvent(itsMessage));
  422.         
  423.         fi.Success();
  424.     }
  425.     else    // Recover
  426.     {
  427.         fMessage = (TAppleEvent *)FreeIfObject(fMessage);
  428.         fReply = (TAppleEvent *)FreeIfObject(fReply);
  429.         fi.ReSignal();
  430.     }
  431. }
  432.  
  433. //--------------------------------------------------------------------------------------------------
  434. #pragma segment MASelCommand
  435.  
  436. pascal void TAppleCommand::Initialize(void)        // override 
  437. {
  438.     inherited::Initialize();
  439.  
  440.     // By default, commands that handle AppleEvents are not undoable.
  441.     // Does it ever make sense for them to be undoable ???  
  442.     fCanUndo = FALSE;
  443.     
  444.     fMessage = NULL;
  445.     fReply = NULL;
  446. }
  447.  
  448. //--------------------------------------------------------------------------------------------------
  449. #pragma segment MAClose
  450.  
  451. pascal void TAppleCommand::Free(void)            // override 
  452. {
  453.     if (fMessage && fReply)
  454.     {
  455.         AppleEvent theMessage = fMessage->fMessage;
  456.         AppleEvent theReply = fReply->fMessage;
  457.         
  458.         // Inform the AppleEvent manager that we are through processing the event. If an error
  459.         // has occurred the default reply will contain the error number and error string.
  460.         AEResumeTheCurrentEvent(theMessage, theReply, (EventHandlerProcPtr)kAENoDispatch, 0);
  461.         
  462.         fMessage = (TAppleEvent *)FreeIfObject(fMessage);
  463.         fReply = (TAppleEvent *)FreeIfObject(fReply);
  464.     }
  465.  
  466.     inherited::Free();
  467. }
  468.  
  469. //--------------------------------------------------------------------------------------------------
  470. #pragma segment MACommandRes
  471.  
  472. pascal void TAppleCommand::ReportError(OSErr error, long)
  473. {
  474.     if (fReply)
  475.     {
  476.         Str255 errStr;
  477.         
  478.         // Inform the AppleEvent manager that an error occured while processing
  479.         // the event. We will try to be as helpful as possible as to what went
  480.         // wrong by returning the error number and the best possible string. The
  481.         // ResumeEvent will occur when the command is freed.
  482.         
  483.         LookupErrString(error, errReasonID, errStr);
  484.         fReply->WriteLong('errn', (long)error);
  485.         fReply->WriteString('errs', errStr);
  486.     }
  487. }
  488.  
  489. //--------------------------------------------------------------------------------------------------
  490. #pragma segment MACommandRes
  491.  
  492. pascal void TAppleCommand::SendIt(void)
  493. {
  494. }
  495.  
  496. //--------------------------------------------------------------------------------------------------
  497. #pragma segment MAFields
  498.  
  499. pascal void TAppleCommand::Fields(TObject* obj)    // override 
  500. {
  501.     obj->DoToField("TAppleCommand", (Ptr)NULL, bClass);
  502.     obj->DoToField("fMessage", (Ptr) & fMessage, bObject);
  503.     obj->DoToField("fReply", (Ptr) & fReply, bObject);
  504.  
  505.     inherited::Fields(obj);
  506. }
  507.  
  508.  
  509.